package com.tql.hualala.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.lang.reflect.Type;

public class ResponseFormatter {

    public static <T> T format(String response, Class<T> clz) {
        try {
            TypeReference<T> tr = new TypeReference<T>() {
                @Override
                public Type getType() {
                    return clz;
                }
            };

            return JSONObject.parseObject(response, tr.getType());
        } catch (Exception e) {
            return null;
        }
    }

//    public static void main(String[] args) {
//        String jsonString = "{\"name\":\"张三\",\"age\":30}";
//        boolean isValid = isJson(jsonString);
//        System.out.println("字符串是否为 JSON 格式：" + isValid);
//    }
//
//    public static boolean isJson(String str) {
//        try {
//            new JSONTokener(str).nextValue();
//            return true;
//        } catch (JSONException e) {
//            return false;
//        }
//    }

}
